package net.chasing.retrofit.util;

import net.chasing.retrofit.RetrofitConstant;
import net.chasing.retrofit.bean.base.ResponseResult;
import net.chasing.retrofit.callback.AbstractResponseCallback;
import net.chasing.retrofit.callback.ResponseBodyCallback;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subscribers.ResourceSubscriber;
import okhttp3.ResponseBody;

/**
 * 通过subscriber.dispose();进行取消订阅
 */
public class RxUtil {
    //基础请求使用
    public static <T> ResourceSubscriber<ResponseResult<T>> toSubscribe(Flowable<ResponseResult<T>> o, AbstractResponseCallback<T> abstractCallback) {
        return toSubscribe(o, 0, abstractCallback);
    }

    public static <T> ResourceSubscriber<ResponseResult<T>> toSubscribe(Flowable<ResponseResult<T>> o, int retryCount,
                                                                        AbstractResponseCallback<T> abstractCallback) {
        ResourceSubscriber<ResponseResult<T>> subscriber = new ResourceSubscriber<ResponseResult<T>>() {
            boolean isPassOnNext = false;

            @Override
            public void onNext(ResponseResult<T> response) {
                try {
                    isPassOnNext = true;
                    if (RetrofitConstant.isSuccess(response.getCode()))
                        abstractCallback.onSuccess(response);
                    else
                        abstractCallback.onFailure(response);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        abstractCallback.onFailure(response);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                } finally {
                    try {
                        abstractCallback.onPostReq();
                    } catch (Exception ignored) {
                    }
                }
            }

            @Override
            public void onError(Throwable t) {
                try {
                    abstractCallback.onFailure(0, t.getMessage());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        abstractCallback.onPostReq();
                    } catch (Exception ignored) {
                    }
                }
            }

            @Override
            public void onComplete() {
                try {
                    if (!isPassOnNext)
                        abstractCallback.onCancel();
                    isPassOnNext = false;
                } catch (Exception ignored) {
                }
            }
        };
        try {
            abstractCallback.onPreReq();
        } catch (Exception ignored) {
        }
        o.retry(retryCount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return subscriber;
    }

    //下载文件使用
    public static ResourceSubscriber<ResponseBody> toSubscribeResponseBodyFlowable(Flowable<ResponseBody> bodyFlowable, final ResponseBodyCallback abstractCallback) {
        try {
            abstractCallback.onPreReq();
        } catch (Exception ignored) {
        }
        return bodyFlowable.retry(0)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<ResponseBody>() {
                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            abstractCallback.onSuccess(responseBody);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                abstractCallback.onPostReq();
                            } catch (Exception ignored) {
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        try {
                            abstractCallback.onFailure(t.getMessage());
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                abstractCallback.onPostReq();
                            } catch (Exception ignored) {
                            }
                        }
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}